<template>
	<div>
		<Card shadow>
			<Form ref="submitForm" label-position="top">
				<FormItem label="条件">
					<Row type="flex" justify="space-between">
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">挂号日期:</p>
								</Col>
								<Col>
									<DatePicker type="daterange" v-model="timers" placeholder="请选择"></DatePicker>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">筛选类型:</p>
								</Col>
								<Col>
									<RadioGroup v-model="params.hospital_time_type">
										<Radio label="1">来院日期</Radio>
										<Radio label="2">未到日期</Radio>
									</RadioGroup>
								</Col>
								<Col v-if="params.hospital_time_type">
									<DatePicker type="daterange" v-model="hospitaltimer" placeholder="请选择"></DatePicker>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">患者状态:</p>
								</Col>
								<Col>
									<Select v-model="outpatient_type_id" multiple style="width:200px">
										<Option v-for="item in outpatient_type_list" :value="item.id" :key="item.id">{{ item.name }}</Option>
									</Select>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">病种:</p>
								</Col>
								<Col>
									<Select v-model="patient_diseases_id" multiple style="width:200px">
										<Option v-for="item in patient_diseases_list" :value="item.id" :key="item.id">{{ item.name }}</Option>
									</Select>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">医助归属人:</p>
								</Col>
								<Col>
									<Select v-model="belong_assistant_id" multiple style="width:200px">
										<Option v-for="item in member_medical_list" :value="item.id" :key="item.id">{{ item.name }}</Option>
									</Select>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">客服归属人:</p>
								</Col>
								<Col>
									<Select v-model="belong_member_id" multiple style="width:200px">
										<Option v-for="item in member_customer_list" :value="item.id" :key="item.id">{{ item.name }}</Option>
									</Select>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">308剩余治疗数:</p>
								</Col>
								<Col>
									<InputNumber placeholder="请输入" :max="9999" :min="-9999" :step="1" @on-blur="inputblurmin(1)" v-model="params.laser_count_start" v-width="120" :formatter="ins"></InputNumber>
									-
									<InputNumber placeholder="请输入" :max="9999" :min="-9999" :step="1" @on-blur="inputblurmax(1)" v-model="params.laser_count_end" v-width="120" :formatter="ins"></InputNumber>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">诊室:</p>
								</Col>
								<Col>
									<Select v-model="clinic_id" multiple style="width:200px">
										<Option v-for="item in clinic_list" :value="item.id" :key="item.id">{{ item.name }}</Option>
									</Select>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">回访次数:</p>
								</Col>
								<Col>
									<InputNumber placeholder="请输入" :max="999" :min="0" :step="1" @on-blur="inputblurmin(2)" v-model="params.callback_count_start" v-width="120" :formatter="ins"></InputNumber>
									-
									<InputNumber placeholder="请输入" :max="999" :min="0" :step="1" @on-blur="inputblurmax(2)" v-model="params.callback_count_end" v-width="120" :formatter="ins"></InputNumber>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">复诊次数:</p>
								</Col>
								<Col>
									<InputNumber placeholder="请输入" :max="999" :min="0" :step="1" @on-blur="inputblurmin(3)" v-model="params.revisiting_count_start" v-width="120" :formatter="ins"></InputNumber>
									-
									<InputNumber placeholder="请输入" :max="999" :min="0" :step="1" @on-blur="inputblurmax(3)" v-model="params.revisiting_count_end" v-width="120" :formatter="ins"></InputNumber>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">未到天数:</p>
								</Col>
								<Col>
									<InputNumber placeholder="请输入" :max="999" :min="0" :step="1" @on-blur="inputblurmin(4)" v-model="params.missed_days_start" v-width="120" :formatter="ins"></InputNumber>
									-
									<InputNumber placeholder="请输入" :max="999" :min="0" :step="1" @on-blur="inputblurmax(4)" v-model="params.missed_days_end" v-width="120" :formatter="ins"></InputNumber>
								</Col>
							</Row>
						</Col>
						<Col span="12" class="ivu-mt-8">
							<Row type="flex" justify="start" :gutter="12">
								<Col v-width="120">
									<p style="text-align: end;">住院次数:</p>
								</Col>
								<Col>
									<InputNumber placeholder="请输入" :max="999" :min="0" :step="1" @on-blur="inputblurmin(5)" v-model="params.hospital_count_start" v-width="120" :formatter="ins"></InputNumber>
									-
									<InputNumber placeholder="请输入" :max="999" :min="0" :step="1" @on-blur="inputblurmax(5)" v-model="params.hospital_count_end" v-width="120" :formatter="ins"></InputNumber>
								</Col>
							</Row>
						</Col>
					</Row>
				</FormItem>
				<FormItem label="导出字段">
					<Checkbox
						:indeterminate="indeterminate"
						:value="checkAll"
						@click.prevent.native="handleCheckAll">全选</Checkbox>
					<CheckboxGroup v-model="field" @on-change="checkAllGroupChange">
						<Checkbox :label="item.id" v-for="item in field_list" :disabled="item.id === '1,2,3'||item.id === '6'" :key="item.id">{{item.name}}</Checkbox>
					</CheckboxGroup>
				</FormItem>
			</Form>
		</Card>
		<FooterToolbar>
			<Button
				class="v-mt-20"
				size="large"
				@click="handleOpenDrawer(1)"
				>重置</Button
			>
			<Button
				type="primary"
				class="v-mt-20"
				size="large"
				@click="handleOpenDrawer(2)"
				>导出</Button
			>
		</FooterToolbar>
		<Modal
			:mask-closable="false"
			:footer-hide="true"
			v-model="modal1"
			title="数据导出"
			@on-cancel="cancel"
			>
			<p>
				数据正在导出，
				<span style="font-size:16px;color: #f90;">{{ time }}</span>
				秒后页面将跳转至数据导出日志页面
			</p>
		</Modal>
	</div>
</template>

<script>
import dayjs from "dayjs";
import {
	exportselectinfo,
	exportexportexcel
} from "@/api/export.js";
export default {
	data() {
		return {
			field_list: [],
			patient_diseases_list: [],
			member_customer_list: [],
			member_medical_list: [],
			outpatient_type_list: [],
			clinic_list: [],
			timers:['',''],
			time: 10,
			hospitaltimer:['',''],
			indeterminate: true,
            checkAll: false,
			field:['1,2,3','6'],
			outpatient_type_id:[],
			belong_assistant_id:[],
			belong_member_id:[],
			patient_diseases_id:[],
			clinic_id:[],
      		timer: null,
     		modal1: false,
			params: {
				field:'',
				reg_start:'',
				reg_end:'',
				outpatient_type_id:'',
				belong_assistant_id:'',
				belong_member_id:'',
				callback_count_start:null,
				callback_count_end:null,
				missed_days_start:null,
				missed_days_end:null,
				patient_diseases_id:'',
				clinic_id:'',
				revisiting_count_start:null,
				revisiting_count_end:null,
				hospital_count_start:null,
				hospital_count_end:null,
				laser_count_start:null,
				laser_count_end:null,
				hospital_time_start:'',
				hospital_time_end:'',
				hospital_time_type:''
			},
		}
	},
	mounted() {
		this.initial()
	},
	methods: {
		initial() {
			this.askData()
		},
		askData(){
			exportselectinfo().then(res=>{
				this.field_list = res.field_list
				this.patient_diseases_list = res.info.patient_diseases_list
				this.member_customer_list = res.info.member_customer_list
				this.member_medical_list = res.info.member_medical_list
				this.outpatient_type_list = res.info.outpatient_type_list
				this.clinic_list = res.info.clinic_list
			})
		},
		// 数字输入框为正整数
		ins(e) {
			return parseInt(e)
		},
		cancel() {
			this.modal1 = false;
			this.timer && clearInterval(this.timer);
			this.time = 10;
		},
		handleCheckAll () {
			if (this.indeterminate) {
				this.checkAll = true;
			} else {
				this.checkAll = !this.checkAll;
			}
			this.indeterminate = !this.checkAll;

			if (this.checkAll) {
				this.field = []
				for (let item of this.field_list) {
					this.field.push(item.id)
				}
			} else {
				this.field = ['1,2,3','6']
			}
		},
		inputblurmin(num){
			if (num === 1) {
				if (this.params.laser_count_end && this.params.laser_count_start >= this.params.laser_count_end) {
					this.$Message.error('请输入合理的308剩余治疗数')
					this.params.laser_count_start = null
				}
			} else if (num === 2) {
				if (this.params.callback_count_end && this.params.callback_count_start >= this.params.callback_count_end) {
					this.$Message.error('请输入合理的回访次数')
					this.params.callback_count_start = null
				}
			} else if (num === 3) {
				if (this.params.revisiting_count_end && this.params.revisiting_count_start >= this.params.revisiting_count_end) {
					this.$Message.error('请输入合理的复诊次数')
					this.params.revisiting_count_start = null
				}
			} else if (num === 4) {
				if (this.params.missed_days_end && this.params.missed_days_start >= this.params.missed_days_end) {
					this.$Message.error('请输入合理的未到天数')
					this.params.missed_days_start = null
				}
			} else if (num === 5) {
				if (this.params.hospital_count_end && this.params.hospital_count_start >= this.params.hospital_count_end) {
					this.$Message.error('请输入合理的住院次数')
					this.params.hospital_count_start = null
				}
			}
		},
		inputblurmax(num){
			if (num === 1) {
				if (this.params.laser_count_start && this.params.laser_count_start >= this.params.laser_count_end) {
					this.$Message.error('请输入合理的308剩余治疗数')
					this.params.laser_count_end = null
				}
			} else if (num === 2) {
				if (this.params.callback_count_start && this.params.callback_count_start >= this.params.callback_count_end) {
					this.$Message.error('请输入合理的回访次数')
					this.params.callback_count_end = null
				}
			} else if (num === 3) {
				if (this.params.revisiting_count_start && this.params.revisiting_count_start >= this.params.revisiting_count_end) {
					this.$Message.error('请输入合理的复诊次数')
					this.params.revisiting_count_end = null
				}
			} else if (num === 4) {
				if (this.params.missed_days_start && this.params.missed_days_start >= this.params.missed_days_end) {
					this.$Message.error('请输入合理的未到天数')
					this.params.missed_days_end = null
				}
			} else if (num === 5) {
				if (this.params.hospital_count_start && this.params.hospital_count_start >= this.params.hospital_count_end) {
					this.$Message.error('请输入合理的住院次数')
					this.params.hospital_count_end = null
				}
			}
		},
		checkAllGroupChange (data) {
			if (data.length === this.field_list.length) {
				this.indeterminate = false;
				this.checkAll = true;
			} else if (data.length > 0) {
				this.indeterminate = true;
				this.checkAll = false;
			} else {
				this.indeterminate = false;
				this.checkAll = false;
			}
		},
		handleOpenDrawer(e) {
			if (e === 2) {
				// if ((!this.params.laser_count_start && this.params.laser_count_end)||(this.params.laser_count_start && !this.params.laser_count_end)) {
				// 	this.$Message.error('请输入合理的308剩余治疗数')
				// 	return
				// }
				// if ((!this.params.callback_count_start && this.params.callback_count_end)||(this.params.callback_count_start && !this.params.callback_count_end)) {
				// 	this.$Message.error('请输入合理的回访次数')
				// 	return
				// }
				// if ((!this.params.revisiting_count_start && this.params.revisiting_count_end)||(this.params.revisiting_count_start && !this.params.revisiting_count_end)) {
				// 	this.$Message.error('请输入合理的复诊次数')
				// 	return
				// }
				// if ((!this.params.missed_days_start && this.params.missed_days_end)||(this.params.missed_days_start && !this.params.missed_days_end)) {
				// 	this.$Message.error('请输入合理的未到天数')
				// 	return
				// }
				// if ((!this.params.hospital_count_start && this.params.hospital_count_end)||(this.params.hospital_count_start && !this.params.hospital_count_end)) {
				// 	this.$Message.error('请输入合理的住院次数')
				// 	return
				// }
				this.modal1 = true;
				this.params.reg_start = this.timers[0]?dayjs(this.timers[0]).format('YYYY-MM-DD'):''
				this.params.reg_end = this.timers[1]?dayjs(this.timers[1]).format('YYYY-MM-DD'):''
				this.params.hospital_time_start = this.hospitaltimer[0]?dayjs(this.hospitaltimer[0]).format('YYYY-MM-DD'):''
				this.params.hospital_time_end = this.hospitaltimer[1]?dayjs(this.hospitaltimer[1]).format('YYYY-MM-DD'):''
				this.params.field = this.field.join(',')
				this.params.outpatient_type_id = this.outpatient_type_id.join(',')
				this.params.belong_assistant_id = this.belong_assistant_id.join(',')
				this.params.belong_member_id = this.belong_member_id.join(',')
				this.params.patient_diseases_id = this.patient_diseases_id.join(',')
				this.params.clinic_id = this.clinic_id.join(',')
				exportexportexcel(this.params).then(res=>{
					this.timer = setInterval(() => {
						this.time = this.time - 1;
						if (this.time <= 0) {
							clearInterval(this.timer);
							this.modal1 = false;
							this.time = 10;
							this.$router.push("/home/setting/managementData/exportLog");
						}
					}, 1000);
				}).catch(()=>{
					this.cancel()
					this.$Message.error();("导出失败");
				})
			}else if (e === 1) {
				this.timers = ['','']
				this.hospitaltimer =['','']
				this.field =['1,2,3','6']
				this.outpatient_type_id =[]
				this.belong_assistant_id =[]
				this.belong_member_id =[]
				this.patient_diseases_id =[]
				this.clinic_id =[]
				this.params = {
					field:'',
					reg_start:'',
					reg_end:'',
					outpatient_type_id:'',
					belong_assistant_id:'',
					belong_member_id:'',
					callback_count_start:null,
					callback_count_end:null,
					missed_days_start:null,
					missed_days_end:null,
					patient_diseases_id:'',
					clinic_id:'',
					revisiting_count_start:null,
					revisiting_count_end:null,
					hospital_count_start:null,
					hospital_count_end:null,
					laser_count_start:null,
					laser_count_end:null,
					hospital_time_start:'',
					hospital_time_end:'',
					hospital_time_type:''
				}
			}
		}
	},
};
</script>
